package com.store.service;

import com.store.dao.PossubmitorderMapper;
import com.store.model.PossubmitorderModel;
import com.store.model.ResultData;
import com.store.utils.ConstantVariable;
import com.store.utils.DateUtil;
import com.store.utils.ExcelUtil;
import org.apache.poi.xssf.usermodel.XSSFWorkbook;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;
import java.util.ArrayList;
import java.util.UUID;

@Service("possubmitorderService")
public class PossubmitorderService {

    @Autowired
    private PossubmitorderMapper possubmitorderMapper;

    @Autowired
    private ExcelUtil<PossubmitorderModel> excelUtil;

    public XSSFWorkbook export(PossubmitorderModel model) {
        model.setPageSize(Integer.MAX_VALUE);
        model.setPageNow(0);
        List<PossubmitorderModel> dataList = getList(model);
        return excelUtil.exportExcel(model, dataList);
    }

    public ResultData getData(PossubmitorderModel model) {
        ResultData returnData = new ResultData();
        try {
            returnData.setDataList(this.getList(model));
            int dataCount = this.getCount(model);
            returnData.setPageCount(dataCount, model.getPageSize());
            returnData.setDataCount(dataCount);
            returnData.setState(ConstantVariable.SUCCESS);
            returnData.setPageNumber(model.getPageNumber());
        } catch (Exception e) {
            returnData.setState(ConstantVariable.ERROR);
            returnData.setMsg(e.getMessage());
        }
        return returnData;
    }

    public PossubmitorderModel getModelById(PossubmitorderModel model) {
        List<PossubmitorderModel> dataList = getList(model);
        if (dataList.size() > 0) {
            return dataList.get(0);
        }
        return null;
    }

    public List<PossubmitorderModel> getList(PossubmitorderModel model) {
        return possubmitorderMapper.getList(model);
    }

    public int getCount(PossubmitorderModel model) {
        return possubmitorderMapper.getCount(model);
    }

    public PossubmitorderModel insert(PossubmitorderModel model) {
        possubmitorderMapper.insert(model);
        return model;
    }

    public PossubmitorderModel addNormalOrder(String orderNo) {
        PossubmitorderModel possubmitorderModel = new PossubmitorderModel();
        possubmitorderModel.setOrderno(orderNo);
        possubmitorderModel.setId(UUID.randomUUID().toString());
        possubmitorderModel.setDatatype(ConstantVariable.ORDER_DATA_TYPE_NORMAL);
        possubmitorderModel.setOrderstatus(ConstantVariable.ORDER_NO_PLAY);
        possubmitorderModel.setCreatetime(DateUtil.getTime());
        possubmitorderMapper.insert(possubmitorderModel);
        return possubmitorderModel;
    }

    public PossubmitorderModel update(PossubmitorderModel model) {
        possubmitorderMapper.update(model);
        return model;
    }

    public List<PossubmitorderModel> batchUpdate(List<PossubmitorderModel> modelList) {
        for (PossubmitorderModel model : modelList) {
            possubmitorderMapper.update(model);
        }
        return modelList;
    }

    public void disableOrEnable(PossubmitorderModel model) {
        possubmitorderMapper.disableOrEnable(model);
    }

    public void delete(PossubmitorderModel model) {
        List<String> idList = new ArrayList<>();
        idList.add(model.getId());
        model.setIdList(idList);
        possubmitorderMapper.delete(model);
    }

    public String batchDelete(List<String> idList) {
        PossubmitorderModel model = new PossubmitorderModel();
        model.setIdList(idList);
        possubmitorderMapper.delete(model);
        return ResultData.toSuccessString();
    }

}